package cn.itsource.service.impl;

import cn.itsource.domain.Admin;
import cn.itsource.exception.BusinessExcpetion;
import cn.itsource.mapper.AdminMapper;
import cn.itsource.service.AdminService;
import jdk.nashorn.internal.ir.ReturnNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin findusername(String username, String password) {
        //查询用户名这条数据是否存在
        Admin admin = adminMapper.findusername(username);
        if (admin == null) { //判断用户名是否存在
            throw new BusinessExcpetion("恁输入用户名错误！请重新输入！");
        } else {
            if (!admin.getPassword().equals(password)) {//用户名存在，判断密码是否正确。
                throw new BusinessExcpetion("恁输入密码错误！请重新输入！");
            } else {
                return admin;
            }
        }

    }

    @Override
    public Admin uppassword(Admin admin, String oldPassword, String newPassword, String reNewPassword) {

        System.out.println(newPassword);
        System.out.println(reNewPassword);

        Admin Admin = adminMapper.findByid(admin.getId());
        if (oldPassword !=null){
            String password = admin.getPassword();
            if (password.equals(oldPassword)){
                  if (newPassword.equals(reNewPassword)){
                       adminMapper.updateAdmin(admin.getId());
                      return admin;
                  }else {
                      throw new BusinessExcpetion("恁输入的两次密码不准确！请输入密码。");
                  }
            }else {
                throw new BusinessExcpetion("恁输入密码不准确！请输入密码。");
            }
        }else {
            throw new BusinessExcpetion("恁输入密码为空！请输入密码。");
        }

    }



    /*@Override
    public Admin findpsaa(String oldPassword, String newPassword, String reNewPasswor) {
        Admin findpsaa = adminMapper.findpsaa(oldPassword);
        if (findpsaa != null){
            String password = findpsaa.getPassword();
            if (password == oldPassword){
                 if(newPassword == reNewPasswor){
                     Admin admin = adminMapper.updatePass(oldPassword, newPassword);
                     return admin;
                 }else {
                     throw new BusinessExcpetion("恁输入的两次密码不准确！请输入密码。");
                 }
            }else {
                throw new BusinessExcpetion("恁输入密码不准确！请输入密码。");
            }
        }else {
            throw new BusinessExcpetion("恁输入密码为空！请输入密码。");
        }

    }*/

}
